package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.LoginSysUserDep;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;


/**
 * 登录验证
 *
 * @author ess
 */
@Api("登录验证")
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysUserService sysUserService;


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        if (loginBody.getBirnum() != null && !"".equals(loginBody.getBirnum())) {
            redisCache.setCacheObject(loginBody.getUsername(), loginBody.getDepid() + "-" + loginBody.getBirnum());
        } else {
            loginBody.setBirnum("");
            redisCache.setCacheObject(loginBody.getUsername(), loginBody.getDepid());
        }

        AjaxResult ajax = AjaxResult.success();
        // 生成令牌

        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid(),loginBody.getUkey_token(),loginBody.getBirnum());

        if (token.contains(Constants.UPDATE_PASSWD)) {
            ajax.put(Constants.USERID, token.split("@")[1]);
            ajax.put(Constants.TOKEN, token.split("@")[0]);
            return ajax;
//            ukey登录
        } else if (token.contains( Constants.UK_LOGIN)) {
            ajax.put(Constants.UK_UUID, token.split("@")[2]);
            ajax.put(Constants.UK_CERT, token.split("@")[1]);
            ajax.put(Constants.TOKEN, token.split("@")[0]);
            return ajax;
        } else {
            ajax.put(Constants.TOKEN, token);
            return ajax;
        }
    }

    /**
     * 根据姓名查询单位列表 登录页面
     */
    @ApiOperation("根据姓名查询单位列表 ")
    @ApiImplicitParam(name = "userName", value = "用户姓名", required = true, dataType = "String", dataTypeClass = String.class)
    @PostMapping(value = "/depListByUserName")
    public AjaxResult depListByUserName(@RequestBody String userName) {
        AjaxResult ajax = AjaxResult.success();
        List<LoginSysUserDep> loginSysUserDeps = sysUserService.selectDepListByUserName(userName);
        ajax.put("depList", loginSysUserDeps);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

}
